///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	eTLiteralUI.m
//	SUMMARY:	Implementation of an inspector for eTLiteral
//	SUPERCLASS:	eTLiteralUI:eTImageUI:Object
//	INTERFACE:	eTLiteralUI.nib
//	PROTOCOLS:	<Inspectable>
//	AUTHOR:		Rohit Khare and Tom Zavisca
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//  IMPLEMENTATION COMMENTS
//		See the overrides for the <Inspectable> API.
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	07/24/94:	Created. First actual implementation.
///////////////////////////////////////////////////////////////////////////////

#import "eTLiteralUI.h"

@implementation eTLiteralUI
// 	id 		literalPanel;
// 	id		literalView;
// 	id		etLiteral;
// 	id		repText;
// 	int		currentFmt;
// 	BOOL	textDirty;

+new
{
	static eTLiteralUI *ui = nil;
	
	if (!ui) {
		ui = [[eTLiteralUI alloc] init];
	}
	return ui;
}
- init
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;

	[super init];
	bundle = [NXBundle bundleForClass:[eTLiteralUI class]];
	if ( [bundle getPath:buf forResource:"eTLiteralUI" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: eTLiteralUI");
	}
	literalView = [literalPanel contentView];
	[repText setDelegate:self];
	textDirty=NO;
	currentFmt=HTMD_FMT; // don't worry, activateI.. should be callled :)
	return self;
}
- free {return self;}

////////////////// Inspectable API
#define 	HTML_REP 	NXUniqueString("HTML Representation")
#define		TeXD_REP 	NXUniqueString("LaTeX Representation")
#define		C_REP 		NXUniqueString("C Code Representation")
#define		ASCII_REP 	NXUniqueString("ASCII Representation")

- (const NXAtom *) types
{
	static NXAtom *types = NULL;
	
	if (!types) {
		int i;
		const NXAtom *superTypes = [super types];
		
		for(i=0;superTypes[i];i++);
		i++; // make room for terminating NULL
		types = malloc(i+5 * sizeof(NXAtom));
		types[0] = HTML_REP;
		types[1] = TeXD_REP;
		types[2] = C_REP;
		types[3] = ASCII_REP;
		for(i=0;superTypes[i];i++) types[i+4] = superTypes[i];
		types[i+4] = NULL;
	}
	return types;
}
- resignInspector: (View *) oldInspector ofType: (const char *) type
{
	if (oldInspector == literalView) {
		// we have to swap OUT the old....
		[self resetRep:self];
	} else 
		[super resignInspector:oldInspector ofType:type];
	return self;
}
- activateInspector: (View *) newInspector ofType: (const char *) aType
{
	NXAtom type;
	type = NXUniqueString(aType);
	if (newInspector == literalView) {
		//and ring in the NEW...
		if(type==HTML_REP) [self changeFormatTo:HTMD_FMT];
		else if(type==TeXD_REP) [self changeFormatTo:TeXD_FMT]; 
		else if(type==C_REP) [self changeFormatTo:C_FMT];
		else if(type==ASCII_REP) [self changeFormatTo:ASCII_FMT];
		else	
		NXLogError("Massive Inspector Failure: asked eTLiteralUI to activate %s", type);
		}
		else [super activateInspector:newInspector ofType:type];
	return self;
}
- inspectorForType:(const char *) aType
{
	NXAtom type;
	type = NXUniqueString(aType);
	if((type==HTML_REP) || (type==TeXD_REP) || (type==C_REP) || (type== ASCII_REP))	return literalView;
	else	return [super inspectorForType:type];
}
- (const char *)inspectorTitle {return NXUniqueString("Literal Element");}
////////////////////////

- setAnnotation:newLiteral
	{[self resetRep:self];
	 etLiteral=newLiteral;
	 return [super setAnnotation:newLiteral];}
-resetRep: sender
{
	if(textDirty) {
		[etLiteral 	setRep:currentFmt 
					fromStream:[repText stream] 
					length:[repText textLength]];
		textDirty=NO;
	}
	return self;
}
-changeFormatTo:(int) newFormat
{
	const char *buf;

	currentFmt = newFormat;
	if (buf = [etLiteral theRepForFormat:currentFmt]) {
		[repText setText:buf];
		textDirty = NO;
	} else {
		[repText setText:""];
	}
	return self;
}
-textDidChange: sender
{
	textDirty=YES;
	return self;
}
@end
